home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / net / URL.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  8.7 KB  |  482 lines

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8. import java.net.Proxy.Type;
  9. import java.security.AccessController;
  10. import java.util.Hashtable;
  11. import java.util.StringTokenizer;
  12. import sun.security.action.GetPropertyAction;
  13. import sun.security.util.SecurityConstants;
  14.  
  15. public final class URL implements Serializable {
  16.    static final long serialVersionUID = -7627629688361524110L;
  17.    private static final String protocolPathProp = "java.protocol.handler.pkgs";
  18.    private String protocol;
  19.    private String host;
  20.    private int port;
  21.    private String file;
  22.    private transient String query;
  23.    private String authority;
  24.    private transient String path;
  25.    private transient String userInfo;
  26.    private String ref;
  27.    transient InetAddress hostAddress;
  28.    transient URLStreamHandler handler;
  29.    private int hashCode;
  30.    static URLStreamHandlerFactory factory;
  31.    static Hashtable handlers = new Hashtable();
  32.    private static Object streamHandlerLock = new Object();
  33.  
  34.    public URL(String var1, String var2, int var3, String var4) throws MalformedURLException {
  35.       this(var1, var2, var3, var4, (URLStreamHandler)null);
  36.    }
  37.  
  38.    public URL(String var1, String var2, String var3) throws MalformedURLException {
  39.       this(var1, var2, -1, var3);
  40.    }
  41.  
  42.    public URL(String var1, String var2, int var3, String var4, URLStreamHandler var5) throws MalformedURLException {
  43.       this.port = -1;
  44.       this.hashCode = -1;
  45.       if (var5 != null) {
  46.          SecurityManager var6 = System.getSecurityManager();
  47.          if (var6 != null) {
  48.             this.checkSpecifyHandler(var6);
  49.          }
  50.       }
  51.  
  52.       var1 = var1.toLowerCase();
  53.       this.protocol = var1;
  54.       if (var2 != null) {
  55.          if (var2 != null && var2.indexOf(58) >= 0 && !var2.startsWith("[")) {
  56.             var2 = "[" + var2 + "]";
  57.          }
  58.  
  59.          this.host = var2;
  60.          if (var3 < -1) {
  61.             throw new MalformedURLException("Invalid port number :" + var3);
  62.          }
  63.  
  64.          this.port = var3;
  65.          this.authority = var3 == -1 ? var2 : var2 + ":" + var3;
  66.       }
  67.  
  68.       Parts var8 = new Parts(var4);
  69.       this.path = var8.getPath();
  70.       this.query = var8.getQuery();
  71.       if (this.query != null) {
  72.          this.file = this.path + "?" + this.query;
  73.       } else {
  74.          this.file = this.path;
  75.       }
  76.  
  77.       this.ref = var8.getRef();
  78.       if (var5 == null && (var5 = getURLStreamHandler(var1)) == null) {
  79.          throw new MalformedURLException("unknown protocol: " + var1);
  80.       } else {
  81.          this.handler = var5;
  82.       }
  83.    }
  84.  
  85.    public URL(String var1) throws MalformedURLException {
  86.       this((URL)null, var1);
  87.    }
  88.  
  89.    public URL(URL var1, String var2) throws MalformedURLException {
  90.       this((URL)var1, var2, (URLStreamHandler)null);
  91.    }
  92.  
  93.    public URL(URL var1, String var2, URLStreamHandler var3) throws MalformedURLException {
  94.       this.port = -1;
  95.       this.hashCode = -1;
  96.       String var4 = var2;
  97.       int var8 = 0;
  98.       String var9 = null;
  99.       boolean var10 = false;
  100.       boolean var11 = false;
  101.       if (var3 != null) {
  102.          SecurityManager var12 = System.getSecurityManager();
  103.          if (var12 != null) {
  104.             this.checkSpecifyHandler(var12);
  105.          }
  106.       }
  107.  
  108.       try {
  109.          int var6;
  110.          for(var6 = var2.length(); var6 > 0 && var2.charAt(var6 - 1) <= ' '; --var6) {
  111.          }
  112.  
  113.          while(var8 < var6 && var2.charAt(var8) <= ' ') {
  114.             ++var8;
  115.          }
  116.  
  117.          if (var2.regionMatches(true, var8, "url:", 0, 4)) {
  118.             var8 += 4;
  119.          }
  120.  
  121.          if (var8 < var2.length() && var2.charAt(var8) == '#') {
  122.             var10 = true;
  123.          }
  124.  
  125.          char var7;
  126.          for(int var5 = var8; !var10 && var5 < var6 && (var7 = var2.charAt(var5)) != '/'; ++var5) {
  127.             if (var7 == ':') {
  128.                String var16 = var2.substring(var8, var5).toLowerCase();
  129.                if (this.isValidProtocol(var16)) {
  130.                   var9 = var16;
  131.                   var8 = var5 + 1;
  132.                }
  133.                break;
  134.             }
  135.          }
  136.  
  137.          this.protocol = var9;
  138.          if (var1 != null && (var9 == null || var9.equalsIgnoreCase(var1.protocol))) {
  139.             if (var3 == null) {
  140.                var3 = var1.handler;
  141.             }
  142.  
  143.             if (var1.path != null && var1.path.startsWith("/")) {
  144.                var9 = null;
  145.             }
  146.  
  147.             if (var9 == null) {
  148.                this.protocol = var1.protocol;
  149.                this.authority = var1.authority;
  150.                this.userInfo = var1.userInfo;
  151.                this.host = var1.host;
  152.                this.port = var1.port;
  153.                this.file = var1.file;
  154.                this.path = var1.path;
  155.                var11 = true;
  156.             }
  157.          }
  158.  
  159.          if (this.protocol == null) {
  160.             throw new MalformedURLException("no protocol: " + var4);
  161.          } else if (var3 == null && (var3 = getURLStreamHandler(this.protocol)) == null) {
  162.             throw new MalformedURLException("unknown protocol: " + this.protocol);
  163.          } else {
  164.             this.handler = var3;
  165.             int var15 = var2.indexOf(35, var8);
  166.             if (var15 >= 0) {
  167.                this.ref = var2.substring(var15 + 1, var6);
  168.                var6 = var15;
  169.             }
  170.  
  171.             if (var11 && var8 == var6) {
  172.                this.query = var1.query;
  173.                if (this.ref == null) {
  174.                   this.ref = var1.ref;
  175.                }
  176.             }
  177.  
  178.             var3.parseURL(this, var2, var8, var6);
  179.          }
  180.       } catch (MalformedURLException var13) {
  181.          throw var13;
  182.       } catch (Exception var14) {
  183.          throw new MalformedURLException(var14.getMessage());
  184.       }
  185.    }
  186.  
  187.    private boolean isValidProtocol(String var1) {
  188.       int var2 = var1.length();
  189.       if (var2 < 1) {
  190.          return false;
  191.       } else {
  192.          char var3 = var1.charAt(0);
  193.          if (!Character.isLetter(var3)) {
  194.             return false;
  195.          } else {
  196.             for(int var4 = 1; var4 < var2; ++var4) {
  197.                var3 = var1.charAt(var4);
  198.                if (!Character.isLetterOrDigit(var3) && var3 != '.' && var3 != '+' && var3 != '-') {
  199.                   return false;
  200.                }
  201.             }
  202.  
  203.             return true;
  204.          }
  205.       }
  206.    }
  207.  
  208.    private void checkSpecifyHandler(SecurityManager var1) {
  209.       var1.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION);
  210.    }
  211.  
  212.    protected void set(String var1, String var2, int var3, String var4, String var5) {
  213.       synchronized(this) {
  214.          this.protocol = var1;
  215.          this.host = var2;
  216.          this.authority = var3 == -1 ? var2 : var2 + ":" + var3;
  217.          this.port = var3;
  218.          this.file = var4;
  219.          this.ref = var5;
  220.          this.hashCode = -1;
  221.          this.hostAddress = null;
  222.          int var7 = var4.lastIndexOf(63);
  223.          if (var7 != -1) {
  224.             this.query = var4.substring(var7 + 1);
  225.             this.path = var4.substring(0, var7);
  226.          } else {
  227.             this.path = var4;
  228.          }
  229.  
  230.       }
  231.    }
  232.  
  233.    protected void set(String var1, String var2, int var3, String var4, String var5, String var6, String var7, String var8) {
  234.       synchronized(this) {
  235.          this.protocol = var1;
  236.          this.host = var2;
  237.          this.port = var3;
  238.          this.file = var7 == null ? var6 : var6 + "?" + var7;
  239.          this.userInfo = var5;
  240.          this.path = var6;
  241.          this.ref = var8;
  242.          this.hashCode = -1;
  243.          this.hostAddress = null;
  244.          this.query = var7;
  245.          this.authority = var4;
  246.       }
  247.    }
  248.  
  249.    public String getQuery() {
  250.       return this.query;
  251.    }
  252.  
  253.    public String getPath() {
  254.       return this.path;
  255.    }
  256.  
  257.    public String getUserInfo() {
  258.       return this.userInfo;
  259.    }
  260.  
  261.    public String getAuthority() {
  262.       return this.authority;
  263.    }
  264.  
  265.    public int getPort() {
  266.       return this.port;
  267.    }
  268.  
  269.    public int getDefaultPort() {
  270.       return this.handler.getDefaultPort();
  271.    }
  272.  
  273.    public String getProtocol() {
  274.       return this.protocol;
  275.    }
  276.  
  277.    public String getHost() {
  278.       return this.host;
  279.    }
  280.  
  281.    public String getFile() {
  282.       return this.file;
  283.    }
  284.  
  285.    public String getRef() {
  286.       return this.ref;
  287.    }
  288.  
  289.    public boolean equals(Object var1) {
  290.       if (!(var1 instanceof URL)) {
  291.          return false;
  292.       } else {
  293.          URL var2 = (URL)var1;
  294.          return this.handler.equals(this, var2);
  295.       }
  296.    }
  297.  
  298.    public synchronized int hashCode() {
  299.       if (this.hashCode != -1) {
  300.          return this.hashCode;
  301.       } else {
  302.          this.hashCode = this.handler.hashCode(this);
  303.          return this.hashCode;
  304.       }
  305.    }
  306.  
  307.    public boolean sameFile(URL var1) {
  308.       return this.handler.sameFile(this, var1);
  309.    }
  310.  
  311.    public String toString() {
  312.       return this.toExternalForm();
  313.    }
  314.  
  315.    public String toExternalForm() {
  316.       return this.handler.toExternalForm(this);
  317.    }
  318.  
  319.    public URI toURI() throws URISyntaxException {
  320.       return new URI(this.toString());
  321.    }
  322.  
  323.    public URLConnection openConnection() throws IOException {
  324.       return this.handler.openConnection(this);
  325.    }
  326.  
  327.    public URLConnection openConnection(Proxy var1) throws IOException {
  328.       if (var1 == null) {
  329.          throw new IllegalArgumentException("proxy can not be null");
  330.       } else {
  331.          SecurityManager var2 = System.getSecurityManager();
  332.          if (var1.type() != Type.DIRECT && var2 != null) {
  333.             InetSocketAddress var3 = (InetSocketAddress)var1.address();
  334.             if (var3.isUnresolved()) {
  335.                var2.checkConnect(var3.getHostName(), var3.getPort());
  336.             } else {
  337.                var2.checkConnect(var3.getAddress().getHostAddress(), var3.getPort());
  338.             }
  339.          }
  340.  
  341.          return this.handler.openConnection(this, var1);
  342.       }
  343.    }
  344.  
  345.    public final InputStream openStream() throws IOException {
  346.       return this.openConnection().getInputStream();
  347.    }
  348.  
  349.    public final Object getContent() throws IOException {
  350.       return this.openConnection().getContent();
  351.    }
  352.  
  353.    public final Object getContent(Class[] var1) throws IOException {
  354.       return this.openConnection().getContent(var1);
  355.    }
  356.  
  357.    public static void setURLStreamHandlerFactory(URLStreamHandlerFactory var0) {
  358.       synchronized(streamHandlerLock) {
  359.          if (factory != null) {
  360.             throw new Error("factory already defined");
  361.          } else {
  362.             SecurityManager var2 = System.getSecurityManager();
  363.             if (var2 != null) {
  364.                var2.checkSetFactory();
  365.             }
  366.  
  367.             handlers.clear();
  368.             factory = var0;
  369.          }
  370.       }
  371.    }
  372.  
  373.    static URLStreamHandler getURLStreamHandler(String var0) {
  374.       URLStreamHandler var1 = (URLStreamHandler)handlers.get(var0);
  375.       if (var1 == null) {
  376.          boolean var2 = false;
  377.          if (factory != null) {
  378.             var1 = factory.createURLStreamHandler(var0);
  379.             var2 = true;
  380.          }
  381.  
  382.          if (var1 == null) {
  383.             Object var3 = null;
  384.             String var14 = (String)AccessController.doPrivileged(new GetPropertyAction("java.protocol.handler.pkgs", ""));
  385.             if (var14 != "") {
  386.                var14 = var14 + "|";
  387.             }
  388.  
  389.             var14 = var14 + "sun.net.www.protocol";
  390.             StringTokenizer var4 = new StringTokenizer(var14, "|");
  391.  
  392.             while(var1 == null && var4.hasMoreTokens()) {
  393.                String var5 = var4.nextToken().trim();
  394.  
  395.                try {
  396.                   String var6 = var5 + "." + var0 + ".Handler";
  397.                   Class var7 = null;
  398.  
  399.                   try {
  400.                      var7 = Class.forName(var6);
  401.                   } catch (ClassNotFoundException var12) {
  402.                      ClassLoader var9 = ClassLoader.getSystemClassLoader();
  403.                      if (var9 != null) {
  404.                         var7 = var9.loadClass(var6);
  405.                      }
  406.                   }
  407.  
  408.                   if (var7 != null) {
  409.                      var1 = (URLStreamHandler)var7.newInstance();
  410.                   }
  411.                } catch (Exception var13) {
  412.                }
  413.             }
  414.          }
  415.  
  416.          synchronized(streamHandlerLock) {
  417.             Object var16 = null;
  418.             URLStreamHandler var17 = (URLStreamHandler)handlers.get(var0);
  419.             if (var17 != null) {
  420.                return var17;
  421.             }
  422.  
  423.             if (!var2 && factory != null) {
  424.                var17 = factory.createURLStreamHandler(var0);
  425.             }
  426.  
  427.             if (var17 != null) {
  428.                var1 = var17;
  429.             }
  430.  
  431.             if (var1 != null) {
  432.                handlers.put(var0, var1);
  433.             }
  434.          }
  435.       }
  436.  
  437.       return var1;
  438.    }
  439.  
  440.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  441.       var1.defaultWriteObject();
  442.    }
  443.  
  444.    private synchronized void readObject(ObjectInputStream var1) throws IOException, ClassNotFoundException {
  445.       var1.defaultReadObject();
  446.       if ((this.handler = getURLStreamHandler(this.protocol)) == null) {
  447.          throw new IOException("unknown protocol: " + this.protocol);
  448.       } else {
  449.          if (this.authority == null && (this.host != null && this.host.length() > 0 || this.port != -1)) {
  450.             if (this.host == null) {
  451.                this.host = "";
  452.             }
  453.  
  454.             this.authority = this.port == -1 ? this.host : this.host + ":" + this.port;
  455.             int var3 = this.host.lastIndexOf(64);
  456.             if (var3 != -1) {
  457.                this.userInfo = this.host.substring(0, var3);
  458.                this.host = this.host.substring(var3 + 1);
  459.             }
  460.          } else if (this.authority != null) {
  461.             int var2 = this.authority.indexOf(64);
  462.             if (var2 != -1) {
  463.                this.userInfo = this.authority.substring(0, var2);
  464.             }
  465.          }
  466.  
  467.          this.path = null;
  468.          this.query = null;
  469.          if (this.file != null) {
  470.             int var4 = this.file.lastIndexOf(63);
  471.             if (var4 != -1) {
  472.                this.query = this.file.substring(var4 + 1);
  473.                this.path = this.file.substring(0, var4);
  474.             } else {
  475.                this.path = this.file;
  476.             }
  477.          }
  478.  
  479.       }
  480.    }
  481. }
  482.